package service;

import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DemoService {
  private static final Logger logger = Logger.getLogger(DemoService.class);
  private DataSource dataSource;

  public void queryDb() {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
      conn = dataSource.getConnection();
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select * from movie limit 1");
      while (rs.next()) {
        logger.info(String.format("Movie[Id=%d,Title=%s,Director=%s,Genre=%s,Language=%s]",
            rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
        
        System.out.println(String.format("Movie[Id=%d,Title=%s,Director=%s,Genre=%s,Language=%s]",
            rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (stmt != null) {
          stmt.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

}
